<?php
/**
 * 软装设计
 */
namespace app\admin\controller;

use think\Request;
use app\admin\model\Office as OfficeModel;

class Office extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new OfficeModel();
    }

    public function index()
    {
        $data = $this->model->where('isdel', '<>', 1)->field('id,title,pic_s,sort,mtime')->select();

        // return json_encode($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    // public function edit($id)
    // {
    //     $data = $this->model->find($id);

    //     if (Request::instance()->isPOST()) {
    //         // var_dump(input("post."));
    //         // exit;
    //         $param = input('');
    //         $title = trim($param['title']);
            
    //         if (!$title) {
    //             $this->error('标题不可为空');
    //         }

    //         try {
    //             $this->model->save([
    //                 'title' => $title,
    //                 'thumb' => $param['thumb'],
    //                 'sort' => intval($param['sort']),
    //                 'status' => intval($param['status']),
    //                 'mtime' => time(),
    //             ], ['id'=>$id]);
    //         } catch (\Exception $e) {
    //             $msg = $e->getMessage();
                
    //             $this->error("错误代码:" . $msg);
    //         }
    //         $this->success(config('msg.1003'), 'index');
    //     }

    //     $this->assign('data', $data);
    //     return $this->fetch();
    // }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            $list = [];
            if (is_array($id)) {
                foreach ($id as $val) {
                    $list[] = ['id'=>$val, 'isdel'=>1];
                }
            } else {
                $list[] = ['id'=>$id, 'isdel'=>1];
            }

            if ($this->model->saveAll($list)) {
                return ['code'=>2,'msg'=>config('msg.1003')];
            } else {
                $this->error(config('msg.1004'));
            }
        }
    }
}
